﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class frmThemCH : System.Web.UI.Page
{
    CauHoi frCH = new CauHoi();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.GridView1.SelectedIndex = 0;
            txtMaCH.Text = GridView1.SelectedRow.Cells[1].Text;
            txtNoidungCH.Text = GridView1.SelectedRow.Cells[2].Text;
            drplMucdo.SelectedItem.Text = GridView1.SelectedRow.Cells[8].Text;
            txtA.Text = GridView1.SelectedRow.Cells[3].Text;
            txtB.Text = GridView1.SelectedRow.Cells[4].Text;
            txtC.Text = GridView1.SelectedRow.Cells[5].Text;
            txtD.Text = GridView1.SelectedRow.Cells[6].Text;
            drplDAdung.SelectedItem.Text = GridView1.SelectedRow.Cells[7].Text;
        }
    }
    public void Hamdong()
    {
        txtMaCH.Enabled = false;
        txtNoidungCH.Enabled = false;
        txtA.Enabled = false;
        txtB.Enabled = false;
        txtC.Enabled = false;
        txtD.Enabled = false;
        drplDAdung.Enabled = false;
        drplMucdo.Enabled = false;
 
    }
    public void Trangthaibd()
    {
        btnThemCH.Text = "Them CH";
        btnSua.Text = "Sua CH";
        btnHuy.Text = "Huy Thao Tac";
        btnThemCH.Enabled = true;
        btnXoaCH.Enabled = true;
        btnSua.Enabled = true;

        Hamdong();
 
    }
    protected void btnThemCH_Click(object sender, EventArgs e)
    {
        if (btnThemCH.Text == "Them CH")
        {
            txtMaCH.Enabled = true;
            txtNoidungCH.Enabled = true;
            txtA.Enabled = true;
            txtA.Text = null;
            txtB.Enabled = true;
            txtB.Text = null;
            txtC.Enabled = true;
            txtC.Text = null;
            txtD.Enabled = true;
            txtD.Text = null;
            drplDAdung.Enabled = true;
            drplDAdung.Items.Clear();
            drplMucdo.Enabled = true;
            btnThemCH.Text = "Luu CH";
            btnXoaCH.Enabled = false;
            btnSua.Enabled = false;
        }
        else if (btnThemCH.Text == "Luu CH")
        {
            
            string mach = txtMaCH.Text;
            string noidungch = txtNoidungCH.Text;
            string da1 = txtA.Text;
            string da2 = txtB.Text;
            string da3 = txtC.Text;
            string da4 = txtD.Text;
            string dokho = drplMucdo.Text;
            string dapan = drplDAdung.SelectedItem.Text;
            frCH.ThemCHmoi(mach, noidungch, da1, da2, da3, da4, dapan, dokho);
            GridView1.DataBind();
            rfvMaCH.ControlToValidate = txtMaCH.ID;
            //rfvnoidung.ControlToValidate = txtNoidungCH.Text;
           // rfvDaA.ControlToValidate = txtA.Text;
            


            btnThemCH.Text = "Them CH";
            Trangthaibd();
 
        }
    }
    protected void btnXoaCH_Click(object sender, EventArgs e)
    {
        try
        {
            string machf = GridView1.SelectedRow.Cells[1].Text;
            frCH.XoaCH(machf);
            GridView1.DataBind();
        }
        catch { }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtMaCH.Text = GridView1.SelectedRow.Cells[1].Text;
        txtNoidungCH.Text = GridView1.SelectedRow.Cells[2].Text;
        drplMucdo.SelectedItem.Text = GridView1.SelectedRow.Cells[8].Text;
        txtA.Text = GridView1.SelectedRow.Cells[3].Text;
        txtB.Text = GridView1.SelectedRow.Cells[4].Text;
        txtC.Text = GridView1.SelectedRow.Cells[5].Text;
        txtD.Text = GridView1.SelectedRow.Cells[6].Text;

        if (drplDAdung.Items.Count != 0)
        {
            drplDAdung.SelectedItem.Text = GridView1.SelectedRow.Cells[7].Text;
        }
        else
        {
            drplDAdung.Items.Add(new ListItem());
            drplDAdung.SelectedItem.Text = GridView1.SelectedRow.Cells[7].Text;
        }

        Trangthaibd();
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {


        if (btnSua.Text == "Sua CH")
        {
            //txtMaCH.Enabled = true;
            txtNoidungCH.Enabled = true;
            

            txtA.Enabled = true;
            txtB.Enabled = true;
            txtC.Enabled = true;
            txtD.Enabled = true;
            drplDAdung.Enabled = true;
            drplDAdung.Items.Clear();
            drplMucdo.Enabled = true;

            drplDAdung.Items.Add(txtA.Text);
            drplDAdung.Items.Add(txtB.Text);
            drplDAdung.Items.Add(txtC.Text);
            drplDAdung.Items.Add(txtD.Text);

            

            btnSua.Text = "Luu CH";
            btnThemCH.Enabled = false;
            btnXoaCH.Enabled = false;
        }
        else if (btnSua.Text == "Luu CH")
        {
            
            string mach = txtMaCH.Text;
            string noidungch = txtNoidungCH.Text;
            string da1 = txtA.Text;
            string da2 = txtB.Text;
            string da3 = txtC.Text;
            string da4 = txtD.Text;
            string dokho = drplMucdo.Text;
            string dapan = drplDAdung.SelectedItem.Text;
            frCH.SuaCH(mach, noidungch, da1, da2, da3, da4, dapan, dokho);
            GridView1.DataBind();
            btnSua.Text = "Sua CH";
            Trangthaibd();

        }
        
        
    }
   
    public void ThemDAvaodrl(string nd)
    {
        drplDAdung.Items.Add(nd);
        drplDAdung.DataBind();
    }
    
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Trangthaibd();
    }
    protected void txtA_TextChanged(object sender, EventArgs e)
    {
        string nda = txtA.Text;
        if (btnSua.Text == "Luu CH")
        {

            drplDAdung.Items[0].Text = txtA.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            ThemDAvaodrl(nda);
        }
        txtB.Focus();
    }
    protected void txtB_TextChanged(object sender, EventArgs e)
    {
        string nda = txtB.Text;
        if (btnSua.Text == "Luu CH")
        {

            drplDAdung.Items[1].Text = txtB.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            ThemDAvaodrl(nda);
        }
    }

    protected void txtC_TextChanged(object sender, EventArgs e)
    {
        string nda = txtC.Text;
        if (btnSua.Text=="Luu CH")
        {
            
            drplDAdung.Items[2].Text = txtC.Text;
        }
       if(btnThemCH.Text=="Luu CH")
        {
            ThemDAvaodrl(nda);
        }
    }
    protected void txtD_TextChanged(object sender, EventArgs e)
    {
        string nda = txtD.Text;
        if (btnSua.Text == "Luu CH")
        {

            drplDAdung.Items[3].Text = txtD.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            ThemDAvaodrl(nda);
        }
    }
}
